local vars = {
	CurMsg	= "",
	x 		= 50,
	y 		= ScrH() - 150,
	chist 	= {},
	schat	= false,
}

local function SetCurMsg( strText )
	vars.CurMsg = strText;
end
hook.Add("ChatTextChanged", "SetCurMsg", SetCurMsg)

local function StoreText(strName, strText, bDead)
	if( bDead ) then
		table.insert(vars.chist, 1, "DEAD* "..strName..": "..strText)
	elseif( !bDead ) then
		table.insert(vars.chist, 1, strName..": "..strText)
	end
	
	table.remove(vars.chist, 15)
end

local function DrawChatBox()
	if( vars.schat ) then
		draw.RoundedBox(6, vars.x, vars.y - 300, 600, 314, Color(0, 0, 0, 200))
		draw.RoundedBox(6, vars.x - 1, vars.y, 602, 25, Color(255, 255, 255, 200))
		draw.RoundedBox(6, vars.x, vars.y + 1, 600, 23, Color(0, 0, 0, 200))
		
		surface.SetFont("TargetIDSmall")
		surface.SetTextColor(255, 255, 255, 255)
		surface.SetTextPos(vars.x + 6, vars.y + 3)
		surface.DrawText( vars.CurMsg )
	end
	
	for k, v in pairs( vars.chist ) do
		surface.SetFont("TargetIDSmall")
		
		surface.SetTextColor(0, 0, 0)
		surface.SetTextPos(vars.x + 5, vars.y - 7 - (k * 20))
		surface.DrawText( v )

		surface.SetTextColor(255, 255, 255)
		surface.SetTextPos(vars.x + 4, vars.y - 7 - (k * 20))
		surface.DrawText( v )
	end
end
hook.Add("HUDPaint", "drawchat", DrawChatBox)

local function StoreOurText(ply, strText, bTeamOnly, bPlayerIsDead)
	if( ValidEntity( ply ) ) then
		if( bPlayerIsDead ) then
			StoreText(ply:Nick(), strText, true)
		else
			StoreText(ply:Nick(), strText, false)
		end
	else
		StoreText("Console", strText, false)
	end
end
hook.Add("OnPlayerChat", "Store", StoreOurText)

local function ShowChatBox( bTeamSay )
	vars.schat = true;
	
	return true;
end
hook.Add("StartChat", "EnableChatBox", ShowChatBox)

local function HideChatBox()
	vars.schat = false;
	
	return true;
end
hook.Add("FinishChat", "HideChat", HideChatBox)